#include "mysyntaxhignlghter.h"
//自定义语法高亮类的步骤:  1。先继承语法高亮类； 2.重写highlightBlock方法，自定义高亮语法


MySyntaxHignlghter::MySyntaxHignlghter(QTextDocument *parent) :QSyntaxHighlighter(parent)
{

}

void MySyntaxHignlghter::highlightBlock(const QString &text)
{
    QTextCharFormat format;
    format.setFontWeight(QFont::Bold);
    format.setBackground(Qt::red);
    format.setForeground(Qt::green);    //字体颜色

    QString pattern="\\b加油\\b";    //  \b为单词的边界
    QRegExp expression(pattern);
    int index=text.indexOf(expression);

    while(index>=0)
    {
        int lengh=expression.matchedLength();  //匹配到的字符长度
        setFormat(index,lengh,format);
        index=text.indexOf(expression,index+lengh);


    }

}
